package com.xizimall.portal.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
/**
 * 购物车Controller
 * <p>Title: CartController</p>
 * <p>Description: </p>
 * <p>华南理工大学研究生</p> 
 * @author	李隆熹
 * @date	2018年4月27日下午10:31:43
 * @version 1.0
 */
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xizimall.portal.pojo.CartItem;
import com.xizimall.portal.service.CartService;
import com.xizimall.common.pojo.XizimallResult;
@Controller
@RequestMapping("/cart")
public class CartController {
     
	@Autowired
	private CartService cartService; 
	
	@RequestMapping("/add/{itemId}")
	public String addCartItem(@PathVariable Long itemId,@RequestParam(defaultValue="1")Integer num
			,HttpServletRequest request,HttpServletResponse response){
		XizimallResult result = cartService.addCartItem(itemId, num, request, response);
		return "redirect:/cart/success.html";
	}
	@RequestMapping("/success")
	public String showSuccess(){
		return "cartSuccess";
	}
	@RequestMapping("/cart")
	public String showCart(HttpServletRequest request,HttpServletResponse response,Model model){
		List<CartItem> list = cartService.getCartItemList(request, response);
		model.addAttribute("cartList", list);
		return "cart";
	}
	@RequestMapping("/update/num/{itemId}/{num}")
	public String updateNum(@PathVariable Long itemId,@PathVariable Integer num,
			HttpServletRequest request,HttpServletResponse response){
		cartService.updateCartItemNum(itemId, num, request, response);
		return "redirect:/cart/cart.html";
	}
	@RequestMapping("/delete/{itemId}")
	public String deleteCartItem(@PathVariable Long itemId
			,HttpServletRequest request,HttpServletResponse response){
		cartService.deleteCartItem(itemId, request, response);
		return "redirect:/cart/cart.html";
	}
}
